package com.zhouyu.service;

import com.zhouyu.dao.CommonPayRepository;
import com.zhouyu.entity.CommonPayEntity;

import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManagerFactory;

/**
 * @Description:将数据写入到数据
 */
@Service
public class AlipayJpaItemWriter {

  @Autowired
  private EntityManagerFactory entityManager;

  @Autowired
  CommonPayRepository commonPayRepository;

  private RepositoryItemWriterBuilder<CommonPayEntity> repositoryItemWriterBuilder;

  public AlipayJpaItemWriter( ) {
    this.repositoryItemWriterBuilder = new RepositoryItemWriterBuilder();
  }

  public JpaItemWriter<CommonPayEntity> getDbWriter(){
    JpaItemWriter<CommonPayEntity> writer = new JpaItemWriter<>();
    writer.setEntityManagerFactory(entityManager);
    return writer;
  }

  public RepositoryItemWriter<CommonPayEntity> repositoryItemWriter() {
   /* RepositoryItemWriter<CommonPayEntity> peopleRepositoryItemWriter = new RepositoryItemWriter<>();
    peopleRepositoryItemWriter.setRepository(commonPayRepository);
    peopleRepositoryItemWriter.setMethodName("save");*/
    RepositoryItemWriter commonPayRepositoryWriter = repositoryItemWriterBuilder.repository(commonPayRepository).methodName("save").build();
    return commonPayRepositoryWriter;
  }
}
